<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    :title="title"
    :visible="isShowDialog"
    @close="closeDialog"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="addDeptsRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" :disabled="disabled" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in people" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer" class="foot">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartmentDetail, reqEditDeoart } from '@/api/deparments'
export default {
  name: 'AddDepts',
  props: {
    isShowDialog: {
      type: Boolean,
      require: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptsList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义部门名称的校验规则
    // 第一种方法
    // const checkName = (rule, value, callback) => {
    //   console.log(value)
    //   console.log(this.nodeData)
    //   if (!this.nodeData.children) {
    //     this.nodeData.children = []
    //   }
    //   if (this.nodeData.children.length) {
    //     const boo = this.nodeData.children.some(item => item.name === value)
    //     console.log(boo)
    //     boo ? callback(new Error(`已有${value}部门`)) : callback()
    //   } else {
    //     callback()
    //   }
    // }
    // 部门名称校验规则
    const checkName = (rule, value, callback) => {
      console.log(value)
      console.log(this.deptsList)
      console.log(this.nodeData)
      // if (this.form.id && this.nodeData.name === value) {
      //   callback()
      //   return
      // }
      let res = []
      if (this.form.id) {
        res = this.deptsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        res = this.deptsList.filter(item => item.pid === this.nodeData.id)
      }
      const boo = res.some(item => item.name === value)
      boo ? callback(new Error(`已有${value}部门`)) : callback()
      console.log(boo)
    }
    /*
    自定义部门编码校验规则
    */
    const checkCode = (rule, value, callback) => {
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      const boo = this.deptsList.some(item => item.code === value)
      boo ? callback(new Error(`已有${value}部门编码`)) : callback()
    }
    return {
      people: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      addDeptsRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['change', 'blue'] },
          { min: 1, max: 50, message: '长度在 1到 50 个字符', trigger: ['change', 'blue'] },
          // 自定义部门名称的校验规则 同级部门中禁止出现重复的部门
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['change', 'blue'] },
          { min: 1, max: 50, message: '长度在 1到 50 个字符', trigger: ['change', 'blue'] },
          // 自定义部门编码的校验规则 校验所有部门编码不允许重复
          { validator: checkCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['change', 'blue'] },
          { min: 1, max: 50, message: '长度在 1到 50 个字符', trigger: ['change', 'blue'] }
        ]
      }
    }
  },
  computed: {
    // 标题显示
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    },
    // 是否禁用部门编码布尔值
    disabled() {
      return !!this.form.id
    }
  },
  created() {
    // const res = await reqGetEmployeeSimple()
    // console.log(res)
    // this.getEmployeeSimple()
    // 编辑部门
    // this.editDeparment()
  },
  methods: {
    closeDialog() {
      // console.log('关闭了')
      // 不直接改值 违反了单向数据流
      // this.isShowDialog = false
      // 同时父组件关闭弹层
      this.$emit('del-dialog')
      // 关闭对话框 重置表单
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
    },
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      console.log(res)
      this.people = res.data
      console.log(res.data)
    },
    clickSubmit() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.form.id) {
            // console.log('编辑')
            const { message } = await reqEditDeoart(this.form)
            // console.log(res)
            this.$message.success(message)
          } else {
            const res = await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
            console.log(res)
            this.$message.success('添加成功')
          }
          this.$emit('add-depts')
          // this.isShowDialog = false
          // this.isShowDialog
          this.$emit('isShowDialog')
        }
      })
    },
    async editDeparment(id) {
      const res = await reqGetDepartmentDetail(id)
      console.log(res)
      this.form = res.data
    }
  }
}
</script>
<style scoped>
  .foot {
   text-align: center;
  }
</style>
